package com.LCQ.crm.web.listener;

import com.LCQ.crm.settings.domain.DicValue;
import com.LCQ.crm.settings.service.UserService;
import com.LCQ.crm.settings.service.UserServiceImpl;
import com.LCQ.crm.utils.ServiceFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

public class SysInitListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("!!!!!system Init!!!!!");
        ServletContext application =  sce.getServletContext();
        //初始化数据字典
        initDataDictionary(application);
        //初始化stage2Possibility
        initStage2Possibility(application);
    }

    private void initDataDictionary(ServletContext application) {
        UserService userService = (UserService) ServiceFactory.getService(new UserServiceImpl());
        Map<String, List<DicValue>> map = userService.getDic();
        Set<String> keySet = map.keySet();
        for(String key:keySet){
            application.setAttribute(key,map.get(key));
        }
    }

    private void initStage2Possibility(ServletContext application) {
        ResourceBundle rb = ResourceBundle.getBundle("Stage2Possibility");
        Enumeration<String> keys = rb.getKeys();
        Map<String,String> map = new HashMap();
        while (keys.hasMoreElements()){
            String key = keys.nextElement();
            map.put(key,rb.getString(key));
        }
        application.setAttribute("stage2Possibility",map);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
